home *** CD-ROM | disk | FTP | other *** search
/ POINT Software Programming / PPROG1.ISO / pascal / swag / textfile.swg / 0047_Delete duplicate lines in sorted text.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1995-02-28  |  11.8 KB  |  309 lines

  1. {$A+,B-,D-,E-,F-,G-,I-,L-,N-,O-,P-,Q-,R-,S-,T-,V-,X-,Y-}
  2. Program Dup;
  3.    { delete duplicate lines from a sorted text file }
  4.    { Dup file1 file2 }
  5.  
  6. (* Author: Eddy Thilleman
  7.    Donated to the public domain *)
  8.  
  9. {$DEFINE NoPlus}
  10. (* comment the above line if you don't want to remove lines terminated
  11.    by '+' characters *)
  12.  
  13. Uses
  14.   Dos;
  15.  
  16. Type
  17.   string3 = string[3];
  18. Const
  19.   WhiteSpace : string3 = #00#09#255;
  20.  
  21. Const
  22.   NoFAttr : word = $1C; { attributen dir, volume, system }
  23.   FAttr   : word = $23; { readonly-, hidden-, archive attributen }
  24.   BufSize = 16384;      { buffersize 16 KB }
  25.   divisor =  1000;
  26.  
  27. Type
  28.   BufType = array [1..BufSize] of char;
  29.  
  30. Var
  31.   Fname1, Fname2   : string;
  32.   Line1, Line2     : string;
  33.   tmp1 , tmp2      : string;   { temporary vars for lower case comparing }
  34.   OldFile, NewFile : text;
  35.   OldBuf , NewBuf  : BufType;
  36.   tel              : longint;
  37.  
  38.  
  39. function OpenTextFile( var InF: text; name: string; var buffer: BufType ): boolean;
  40. begin
  41.   Assign( InF, Name );
  42.   SetTextBuf( InF, buffer );
  43.   Reset( InF );
  44.   OpenTextFile := ( IOResult = 0 );
  45. end { OpenTextFile };
  46.  
  47. function CreateTextFile( var OutF: text; name: string; var buffer: BufType ): boolean;
  48. begin
  49.   Assign( OutF, Name );
  50.   SetTextBuf( OutF, buffer );
  51.   Rewrite( OutF );
  52.   CreateTextFile := ( IOResult = 0 );
  53. end { CreateTextFile };
  54.  
  55.  
  56. function FileExist( var FName : string ) : Boolean;
  57.   {-Return true if entry is found and if it's a file}
  58. var
  59.   F    : file;
  60.   Attr : Word;
  61. begin
  62.   Assign( F, FName );
  63.   GetFAttr( F, Attr );
  64.   if DosError = 0 then
  65.     FileExist := ( ( Attr and NoFAttr ) = 0 )
  66.     { not dir-, volume- or system bit? }
  67.   else
  68.     FileExist := False;            { DosError }
  69.   {}
  70. end;
  71.  
  72.  
  73. procedure StrCopy( var Str1, Str2: string ); assembler;
  74.   { copy str1 to str2 }
  75. asm
  76.         LDS   SI,Str1    { load in DS:SI pointer to str1 }
  77.         CLD              { string operations forward     }
  78.         LES   DI,Str2    { load in ES:DI pointer to str2 }
  79.         XOR   CH,CH      { clear CH                      }
  80.         MOV   CL,[SI]    { length str1 --> CX            }
  81.         INC   CX         { include length byte           }
  82.     REP MOVSB            { copy str1 to str2             }
  83. end  { StrCopy };
  84.  
  85.  
  86. procedure Lower( var Str: String );
  87. { 52 Bytes by Bob Swart, 11-6-1993, FidoNet '80XXX' FASTEST! }
  88. InLine(
  89.   $8C/$DA/               {       mov   DX,DS                 }
  90.   $BB/Ord('A')/
  91.       Ord('Z')-Ord('A')/ {       mov   BX,'Z'-'A'/'A'        }
  92.   $5E/                   {       pop   SI                    }
  93.   $1F/                   {       pop   DS                    }
  94.   $FC/                   {       cld                         }
  95.   $AC/                   {       lodsb                       }
  96.   $88/$C1/               {       mov   CL,AL                 }
  97.   $30/$ED/               {       xor   CH,CH                 }
  98.   $D1/$E9/               {       shr   CX,1                  }
  99.   $73/$0B/               {       jnc   @Part1                }
  100.   $AC/                   {       lodsb                       }
  101.   $28/$D8/               {       sub   AL,BL                 }
  102.   $38/$F8/               {       cmp   AL,BH                 }
  103.   $77/$04/               {       ja    @Part1                }
  104.   $80/$44/$FF/
  105.       Ord('a')-Ord('A')/ {@Loop: ADD   Byte Ptr[SI-1],'a'-'A'}
  106.   $E3/$14/               {@Part1:jcxz  @Exit                 }
  107.   $AD/                   {       lodsw                       }
  108.   $28/$D8/               {       sub   AL,BL                 }
  109.   $38/$F8/               {       cmp   AL,BH                 }
  110.   $77/$04/               {       ja    @Part2                }
  111.   $80/$44/$FE/
  112.       Ord('a')-Ord('A')/ {       ADD   Byte Ptr[SI-2],'a'-'A'}
  113.   $49/                   {@Part2:dec   CX                    }
  114.   $28/$DC/               {       sub   AH,BL                 }
  115.   $38/$FC/               {       cmp   AH,BH                 }
  116.   $77/$EC/               {       ja    @Part1                }
  117.   $EB/$E6/               {       jmp   @Loop                 }
  118.   $8E/$DA                {@Exit: mov   DS,DX                 }
  119. ) { LowerFast };
  120.  
  121.  
  122. procedure White2Space( var Str: string; const WhiteSpace: string ); assembler;
  123.   { replace white space chars in Str by spaces
  124.     the string WhiteSpace contains the chars to replace }
  125. asm     { setup }
  126.         cld                      { string operations forwards    }
  127.         les   di, str            { ES:DI points to Str           }
  128.         xor   cx, cx             { clear cx                      }
  129.         mov   cl, [di]           { length Str in cl              }
  130.         jcxz  @exit              { if length of Str = 0, exit    }
  131.         inc   di                 { point to 1st char of Str      }
  132.         mov   dx, cx             { store length of Str           }
  133.         mov   bx, di             { pointer to Str                }
  134.         lds   si, WhiteSpace     { DS:SI points to WhiteSpace    }
  135.         mov   ah, [si]           { load length of WhiteSpace     }
  136.  
  137. @start: cmp   ah, 0              { more chars WhiteSpace left?   }
  138.         jz    @exit              { no, exit                      }
  139.         inc   si                 { point to next char WhiteSpace }
  140.         mov   al, [si]           { next char to hunt             }
  141.         dec   ah                 { ah counting down              }
  142.         xor   dh, dh             { clear dh                      }
  143.         mov   cx, dx             { restore length of Str         }
  144.         mov   di, bx             { restore pointer to Str        }
  145.         mov   dh, ' '            { space char                    }
  146. @scan:
  147.   repne scasb                    { the hunt is on                }
  148.         jnz   @next              { white space found?            }
  149.         mov   [di-1], dh         { yes, replace that one         }
  150. @next:  jcxz  @start             { if no more chars in Str       }
  151.         jmp   @scan              { if more chars in Str          }
  152. @exit:
  153. end  { White2Space };
  154.  
  155.  
  156. procedure RTrim( var Str: string ); assembler;
  157.   { remove trailing spaces from str }
  158. asm     { setup }
  159.         std                      { string operations backwards   }
  160.         les   di, str            { ES:DI points to Str           }
  161.         xor   cx, cx             { clear cx                      }
  162.         mov   cl, [di]           { length Str in cl              }
  163.         jcxz  @exit              { if length of Str = 0, exit    }
  164.         mov   bx, di             { bx points to Str              }
  165.         add   di, cx             { start with last char in Str   }
  166.         mov   al, ' '            { hunt for spaces               }
  167.  
  168.         { remove trailing spaces }
  169.    repe scasb                    { the hunt is on                }
  170.         jz    @null              { only spaces?                  }
  171.         inc   cx                 { no, don't lose last char      }
  172. @null:  mov   [bx], cl           { overwrite length byte of Str  }
  173. @exit:
  174. end  { RTrim };
  175.  
  176.  
  177. procedure LTrim( var Str: string ); assembler;
  178.   { remove leading spaces from str }
  179. asm     { setup }
  180.         cld                      { string operations forward          }
  181.         lds   si, str            { DS:SI points to Str                }
  182.         xor   cx, cx             { clear cx                           }
  183.         mov   cl, [si]           { length Str --> cl                  }
  184.         jcxz  @exit              { if length Str = 0, exit            }
  185.         mov   bx, si             { save pointer to length byte of Str }
  186.         inc   si                 { 1st char of Str                    }
  187.         mov   di, si             { pointer to 1st char of Str --> di  }
  188.         mov   al, ' '            { hunt for spaces                    }
  189.         xor   dx, dx             { clear dx                           }
  190.  
  191. @start: { look for leading spaces }
  192.    repe scasb                    { the hunt is on                     }
  193.         jz    @done              { if only spaces, we are done        }
  194.         inc   cx                 { no, don't lose 1st non-blank char  }
  195.         dec   di                 { no, don't lose 1st non-blank char  }
  196.         mov   dx, cx             { new lenght of Str                  }
  197.         xchg  di, si             { swap si and di                     }
  198.     rep movsb                    { move remaining part of Str         }
  199. @done:  mov   [bx], dl           { new length of Str                  }
  200. @exit:
  201. end  { LTrim };
  202.  
  203.  
  204. function LineOK( var str: string ) : Boolean; assembler;
  205.   { Line contains chars > ASCII 20h ? }
  206. asm     { setup }
  207.         xor   ax, ax         { assume false return value        }
  208.         xor   cx, cx         { clear cx                         }
  209.         lds   si, str        { load in DS:SI pointer to Str     }
  210.         mov   cl, [si]       { length Str --> cx                }
  211.         jcxz  @exit          { if no characters, exit           }
  212.         inc   si             { point to 1st character           }
  213.  
  214.         { look for chars > ASCII 20h }
  215. @start: mov   bl, [si]       { load character                   }
  216.         cmp   bl, ' '        { char > ASCII 20h?                }
  217.         ja    @yes           { yes, return true                 }
  218.         inc   si             { next character                   }
  219.         dec   cx             { count down                       }
  220.         jcxz  @exit          { if no more characters left, exit }
  221.         jmp   @start         { try again                        }
  222. @yes:   mov   ax, 1          { return value true                }
  223. @exit:
  224. end  { LineOK };
  225.  
  226.  
  227. procedure TestLine( var Line, tmp : string );
  228. var
  229.   len: byte absolute Line;
  230.  
  231.   procedure TrimLine;
  232.   begin
  233.     White2Space( Line, WhiteSpace );  { white space to spaces   }
  234.     RTrim( Line );                    { remove trailing spaces  }
  235.   end;
  236.  
  237. begin
  238.   TrimLine;
  239.   while not EOF( OldFile ) and ( IOResult = 0 )
  240.   and ((len = 0) or not LineOK( Line )
  241. {$IFDEF NoPlus}
  242.   or (Line[len] = '+')
  243. {$ENDIF}
  244.   ) do
  245.   begin
  246.     ReadLn( OldFile, Line );
  247.   end;
  248.   StrCopy( Line, tmp );             { copy to temp string     }
  249.   LTrim( tmp );                     { remove leading spaces   }
  250.   Lower( tmp );                     { translate to lower case }
  251. end;  { TestLine }
  252.  
  253.  
  254. begin
  255.   if ParamCount > 1 then             { parameters file1 file2 }
  256.   begin
  257.     Fname1 := FExpand( ParamStr( 1 ) );
  258.     Fname2 := FExpand( ParamStr( 2 ) );
  259.     tel := 0;
  260.     tmp1 := '';
  261.     if FileExist( Fname1 ) then
  262.     begin
  263.       if OpenTextFile( OldFile, Fname1, OldBuf ) then
  264.       begin
  265.         if CreateTextFile( NewFile, Fname2, NewBuf ) then
  266.         begin
  267.           ReadLn( OldFile, Line2 );
  268.  
  269.           while not EOF( OldFile ) and ( IOResult = 0 ) do
  270.           begin
  271.             TestLine( Line2, tmp2 );
  272.             if (tmp1 <> tmp2) then
  273.             begin
  274.               StrCopy( Line2, Line1 );         { copy Line2 to Line1 }
  275.               StrCopy( tmp2, tmp1 );           { copy tmp2  to tmp1  }
  276.               WriteLn( NewFile, Line1 );
  277.               inc( tel );
  278.               if (tel mod divisor) = 0 then write( #13, tel, ' unique lines' );
  279.             end;
  280.             ReadLn( OldFile, Line2 );
  281.           end {while not EOF};
  282.  
  283.           TestLine( Line2, tmp2 );
  284.           if (length( Line2 ) > 0) and (tmp1 <> tmp2) then
  285.           begin
  286.             WriteLn( NewFile, Line2 );
  287.             inc( tel );
  288.           end;
  289.  
  290.           writeln( #13, tel, ' unique lines' );
  291.           Close( NewFile );
  292.           Close( OldFile );
  293.         end { if create file2 }
  294.         else
  295.           writeln(' error creating file ', Fname1 );
  296.         { error creating file }
  297.       end { if open file1 }
  298.       else
  299.         writeln(' error opening file ', Fname1 );
  300.       { error opening file }
  301.     end { if FileExist( Fname1 ) }
  302.     else
  303.       writeln( Fname1, ' not found' );
  304.     { file not found }
  305.   end { if ParamCount > 1 }
  306.   else
  307.     Writeln( 'Dup file1 file2' );
  308. end.
  309.